package cn.support.neo.mvp.core.base;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.github.pwittchen.prefser.library.Prefser;

import cn.support.neo.mvp.core.provider.TokenProvider;
import cn.support.neo.mvp.core.service.client.HttpClient;
import retrofit2.Retrofit;

public abstract class BaseModel<T, R extends BaseModel> {

    public Context context;
    public Prefser prefser;
    public T service;

    @Nullable
    public TokenProvider tokenProvider;
    public HttpClient<Retrofit> httpClient;


    public BaseModel(@NonNull Context context,
                     @NonNull HttpClient<Retrofit> httpClient,
                     @Nullable TokenProvider tokenProvider,
                     @NonNull Prefser prefser) {
        this.context = context;
        this.tokenProvider = tokenProvider;
        this.httpClient = httpClient;
        this.httpClient.setToken(tokenProvider);
        this.prefser = prefser;
        this.service = httpClient.getHttpClient().create(getServiceClass());
    }

    protected abstract Class<T> getServiceClass();

    public R setToken(final String token) {
        tokenProvider.setToken(token);
        return (R) this;
    }

    public String getToken() {
        return tokenProvider.getToken();
    }

    public T getService() {
        return service;
    }

}